package fr.labri.harmony.analysis.jsep.results;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringEscapeUtils;

import au.com.bytecode.opencsv.CSVReader;

public class CSVToHTML {
	
	public static void main(String[] args) throws IOException {
		
		List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(22,26,36,40,57,99,105,109,149,150,152,181,184,188,191,197,203,243,273,274,277,278,292,365,396,403,407,429,444,450,455,544,552,566,580,644,650,770,774,776,967,969,990,1036,1106,1254,1391,1406,1424,1454));
		
		String file = "Logs_migrations.csv";
		CSVReader reader = new CSVReader(new InputStreamReader(new FileInputStream(new File(file))), ';', '"', 1);
		String[] nextLine;
		int cpt=0;
		PrintStream ps = new PrintStream("html/Logs_migrations.html");
		for(String line : Files.readAllLines(Paths.get("html/header.html"), Charset.forName("UTF-8")))
			ps.println(line);
		ps.println("<table class='table-striped table-bordered'>");
		ps.println("<tr><td>#ID</td><td>Source</td><td>Target</td><td>Url</td><td>Reason</td></tr>");
		while ((nextLine = reader.readNext()) != null) {
		  if (nextLine != null) {
			  cpt++;
			  if(!numbers.contains(cpt))
				  continue;
		      ps.println("<tr><td>#"+cpt+"</td>");
		      int k = 0;
		      String source = "";
		      String target = "";
		      for(String col : nextLine){
		    	  if(k==0)
		    		  source = col;
		    	  if(k==1)
		    		  target = col;
		    	  if(k==3) {
		    		  col = StringEscapeUtils.escapeHtml4(col).replaceAll(System.getProperty("line.separator"),"<br />\n");
		    		  col = col.replaceAll(source, "\\<font\\ color='red'\\>"+source+"\\<\\/font>");
		    		  col = col.replaceAll(target, "\\<font\\ color='green'\\>"+target+"\\<\\/font>");
		    	  }
		    	  ps.println("<td>"+col+"</td>");
		    	  k++;
		      }
		      ps.println("</tr>");
		  }
		}
		ps.println("</table>");
		for(String line : Files.readAllLines(Paths.get("html/footer.html"), Charset.forName("UTF-8")))
			ps.println(line);
		ps.close();
		reader.close();
		
	}
	
	
}
